package com.ce.pms.charge.service.impl;

import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IBankAccountDao;
import com.ce.pms.charge.dao.IFeeBankDao;
import com.ce.pms.charge.model.BankAccountDomain;
import com.ce.pms.charge.model.BankDomain;
import com.ce.pms.charge.service.IBankAccountService;
import com.ce.pms.charge.vo.BankAccountVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.customer.dao.IProRightDao;
import com.ce.pms.customer.model.ProRightDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

public class BankAccountServiceImpl extends PmsBaseServiceImpl implements IBankAccountService{
	private IBankAccountDao bankAccountDao;

	private IFeeBankDao feeBankDao;

	private IHouseDao houseDao;

	private IProRightDao proRightDao;

	/**
	 * 启用银行帐号
	 *@param bankAccount
	 * @author Adama Yue
	 * @CreateDate:下午04:33:09
	 */
	public void activationBankAccount(BankAccountDomain bankAccount){
		bankAccount.setIsActive(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
		this.bankAccountDao.saveOrUpdateObject(bankAccount);
	}

	/**
	 * 功能描述: 启用指定id的银行帐号 <br>
	 * @param bankAccountId
	 * @param operator
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean activationBankAccount(Long bankAccountId,Long operator){
		super.checkParameterIsNull("common:parameters_null_exception, ", bankAccountId,operator);

		BankAccountDomain bankAccountdo = this.bankAccountDao.findById(bankAccountId);
		if (BlankUtil.isBlank(bankAccountdo)) {
			return false;
		}

		super.modifyInfo(bankAccountdo.getSysInfo(), operator);
		bankAccountdo.setIsActive(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);

		this.bankAccountDao.updateObject(bankAccountdo);

		return true;
	}

	/**
	 * 功能描述: 增加划款银行帐号 <br>
	 * @param bankAccount
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountVO addBankAccount(BankAccountVO bankAccount) {
		try {
			super.checkParameterIsNull(bankAccount, "common:parameters_null_exception, ");
			super.checkCreatorInfo(bankAccount.getCreator());

			BankAccountDomain bankAccountDo = this.assembleV2D(bankAccount);
			bankAccountDo.setIsActive(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);

			bankAccountDo.checkRequiredField();

			this.bankAccountDao.saveObject(bankAccountDo);

			bankAccount.setKeyword(bankAccountDo.getKeyword());
		} catch(BusinessException be){
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(), e);
			throw new BusinessException("charge:charge_bankaccount_add_fatal_exceptin",BusinessException.FAILURE);
		}

		return bankAccount;
	}

	/**
	 * 取得给定房间的当前可用银行帐号
	 *@param house
	 *@return
	 * @author Adama Yue
	 * @CreateDate:下午04:31:46
	 */
	public BankAccountDomain currentBankAccount(HouseDomain house, boolean validity){
		return this.bankAccountDao.getBankAccountByHouseId(house.getKeyword(), validity);
	}

	/**
	 * 功能描述: 取得所有给定房间的当前可用银行帐号 <br>
	 * @param houseIds
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-7-31  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BankAccountDomain> currentBankAccount(String houseIds, boolean validity) {
		return this.bankAccountDao.getBankAccountListByHouseIds(houseIds, validity);
	}

	/**
	 * 禁用银行帐号
	 *@param bankAccount
	 * @author Adama Yue
	 * @CreateDate:下午04:32:32
	 */
	public void disableBankAccount(BankAccountDomain bankAccount){
		bankAccount.setIsActive(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		this.bankAccountDao.saveOrUpdateObject(bankAccount);
	}

	/**
	 * 功能描述: 禁用指定id的银行帐号 <br>
	 * @param bankAccountId
	 * @param operator 操作人id
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean disableBankAccount(Long bankAccountId,Long operator){
		super.checkParameterIsNull("common:parameters_null_exception, ", bankAccountId,operator);

		BankAccountDomain bankAccountdo = this.bankAccountDao.findById(bankAccountId);
		if (BlankUtil.isBlank(bankAccountdo)) {
			return false;
		}

		super.modifyInfo(bankAccountdo.getSysInfo(), operator);
		bankAccountdo.setStopDate(bankAccountdo.getSysInfo().getModifyDate());
		bankAccountdo.setIsActive(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		this.bankAccountDao.updateObject(bankAccountdo);

		return true;
	}

	/**
	 * 功能描述: 删除指定房间的当前有效产权的划款银行账号 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-23  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean deleteBankAccount(Long houseId){
		ProRightDO proRight = this.proRightDao.getEffectiveProRightByHouseId(houseId);
		if (BlankUtil.isBlank(proRight)) {
			return true;
		}
		BankAccountDomain bankAccount = this.bankAccountDao.getBankAccountByProRightId(proRight.getProRightId(), true);
		if (BlankUtil.isBlank(bankAccount)) {
			return true;
		}

		this.bankAccountDao.deleteObject(bankAccount);

		return true;
	}

	/**
	 * 功能描述: 修改划款银行帐号 <br>
	 * @param bankAccount
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountVO editBankAccount(BankAccountVO bankAccount) {
		try {
			super.checkParameterIsNull(bankAccount, "common:parameters_null_exception, ");
			super.checkCreatorInfo(bankAccount.getCreator());

			BankAccountDomain bankAccountDo = this.assembleV2D(bankAccount);

			bankAccountDo.checkRequiredField();

			this.bankAccountDao.updateObject(bankAccountDo);

		} catch(BusinessException be){
			throw be;
		} catch (Exception e) {
			super.logger.fatal(e.getMessage(), e);
			throw new BusinessException("charge:charge_bankaccount_edit_fatal_exceptin",BusinessException.FAILURE);
		}

		return bankAccount;
	}

	/**
	 * 功能描述: 取得给定id的房间的当前可用银行帐号 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountVO getCurrentBankAccount(Long houseId){
		BankAccountDomain bankAccountdo = this.bankAccountDao.getBankAccountByHouseId(houseId, true);
		return this.assembleD2V(bankAccountdo);
	}

	/**
	 * 功能描述: 取得给定id的产权的当前可用银行帐号 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountVO getCurrentBankAccountByPro(Long proRightId){
		BankAccountDomain bankAccountdo = this.bankAccountDao.getBankAccountByProRightId(proRightId, true);
		return this.assembleD2V(bankAccountdo);
	}

	public void setBankAccountDao(IBankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	/**
	 * @param feeBankDao the feeBankDao to set
	 */
	public void setFeeBankDao(IFeeBankDao feeBankDao) {
		this.feeBankDao = feeBankDao;
	}

	/**
	 * @param houseDao the houseDao to set
	 */
	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	/**
	 * @param proRightDao the proRightDao to set
	 */
	public void setProRightDao(IProRightDao proRightDao) {
		this.proRightDao = proRightDao;
	}

	/**
	 * 功能描述:  <br>
	 * @param bankAccountdo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private BankAccountVO assembleD2V(BankAccountDomain bankAccountdo){
		BankAccountVO bankAccountVo = null;
		if (!BlankUtil.isBlank(bankAccountdo)) {
			bankAccountVo = new BankAccountVO();
			BeanUtil.convertVoAsDo(bankAccountdo, bankAccountVo);

			BankDomain bank= bankAccountdo.getBank();
			if (!BlankUtil.isBlank(bank)) {
				bankAccountVo.setBankId(bank.getKeyword());
				bankAccountVo.setBankName(bank.getBankName());
			}

			HouseDomain house = bankAccountdo.getHouse();
			if (!BlankUtil.isBlank(house)) {
				bankAccountVo.setHouseId(house.getKeyword());
				bankAccountVo.setHouseNumber(house.getHouseNumber());
			}

			ProRightDO pro = bankAccountdo.getProRight();
			if (!BlankUtil.isBlank(pro)) {
				bankAccountVo.setProRightId(pro.getProRightId());
			}

			SysInfo sysInfo = bankAccountdo.getSysInfo();//得到创建信息和修改信息
			if (!BlankUtil.isBlank(sysInfo)) {
				bankAccountVo.setCreator(sysInfo.getCreator());//创建人id
				bankAccountVo.setSysdate(sysInfo.getSysDate());//创建日期
				bankAccountVo.setModifier(sysInfo.getModifier());//修改人id
				bankAccountVo.setModifierDate(sysInfo.getModifyDate());//修改日期
			}
		}

		return bankAccountVo;
	}

	/**
	 * 功能描述:  <br>
	 * @param bankAccontVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private BankAccountDomain assembleV2D(BankAccountVO bankAccontVo){
		BankAccountDomain bankAccountDo = this.wangleDomainInstance(bankAccontVo);

		BeanUtil.convertVoAsDo(bankAccontVo, bankAccountDo);

		HouseDomain house = this.houseDao.findById(bankAccontVo.getHouseId());
		bankAccountDo.setHouse(house);

		ProRightDO proRight = this.proRightDao.findById(bankAccontVo.getProRightId());
		bankAccountDo.setProRight(proRight);

		BankDomain bank = this.feeBankDao.findById(bankAccontVo.getBankId());
		bankAccountDo.setBank(bank);

		return bankAccountDo;
	}

	/**
	 * 功能描述:  <br>
	 * @param bankAccontVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-25  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private BankAccountDomain wangleDomainInstance(BankAccountVO bankAccontVo){
		BankAccountDomain bankAccountDo = null;
		SysInfo sysInfo=null;
		if (BlankUtil.isBlank(bankAccontVo.getKeyword())) {
			bankAccountDo = new BankAccountDomain();
			sysInfo= super.createInfo(bankAccontVo.getCreator());
			bankAccountDo.setSysInfo(sysInfo);
			bankAccontVo.setSysdate(sysInfo.getSysDate());
		}else{
			bankAccountDo=this.bankAccountDao.findById(bankAccontVo.getKeyword());
            sysInfo = bankAccountDo.getSysInfo();
			super.modifyInfo(sysInfo, bankAccontVo.getModifier());
			bankAccontVo.setModifierDate(sysInfo.getModifyDate());
		}
		return bankAccountDo;
	}

}
